/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.core.common.utils.CookieUtil
   Module Description   :

   Date Created      : 2010/7/22
   Original Author   : cut7man
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.core.common.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * @author cut7man
 *
 */
public class CookieUtil {

	public static final String NAME_YAODIAN100_MEMBER_ID = "_1.100.D"; // 用戶序號
	public static final String NAME_YAODIAN100_LOGIN_ID = "_1.100.N"; // 用戶名稱
	public static final String NAME_YAODIAN100_LOGIN_ID_STATIC = "_1.100.SN"; // 用戶名稱
	public static final String NAME_YAODIAN100_NICKNAME = "_1.100.L"; // 用戶名稱
	public static final String NAME_YAODIAN100_IMAGE_VALID = "_1.100.IV"; // 用戶名稱
	public static final String NAME_YAODIAN100_APLLY_SOURCE = "_1.100.AS"; // 用戶來源
	public static final String DOMAIN_NAME = "yaodian100.com";
	public static final String CONTEXT_PATH = "/";

	private static final int COOKIE_TIMEOUT = 30 * 60;
	private static final int CARTID_COOKIE_TIMEOUT = 60 * 60 * 24 * 7;// 購物車cookie時間
	private static int time_out = 0;
	private static ThreadLocal<HttpServletResponse> responseInThread=new ThreadLocal<HttpServletResponse>();
	public static void setResponse(HttpServletResponse response){
	    responseInThread.set(response);
	}
	
	public static void clear(String name) {
		Cookie c = new Cookie(name, null);
		if (NAME_YAODIAN100_LOGIN_ID.equals(name) || NAME_YAODIAN100_MEMBER_ID.equals(name)
				|| NAME_YAODIAN100_NICKNAME.equals(name)) {
			c.setDomain(DOMAIN_NAME);
		}
		c.setVersion(0);
		c.setPath(CONTEXT_PATH);
		c.setMaxAge(0); // A zero value causes the cookie to be deleted.
		getResponse().addCookie(c);
	}
	public static String getCookieValue(String name, boolean decode) {
		Cookie[] cookies = getRequest().getCookies();
		if (cookies == null) {
			return null;
		}
		for (int i = 0; i < cookies.length; i++) {
			Cookie c = cookies[i];
			if (name.equals(c.getName())) {
				String value = c.getValue();
				if (decode) {
					return value == null ? "" : PasswordUtils.decodePassword(value, null); // 要decode
				} else {
					return value == null ? "" : value;
				}
			}
		}
		return null; // not found
	}
    public static void add(String name, String value) {
	Cookie c = new Cookie(name, value);
	c.setVersion(0);
	c.setPath(CONTEXT_PATH);
	if (NAME_YAODIAN100_LOGIN_ID.equals(name) || NAME_YAODIAN100_MEMBER_ID.equals(name)
			|| NAME_YAODIAN100_NICKNAME.equals(name) || name.equals("LTINFO")) {
		c.setDomain(DOMAIN_NAME);
	}
	if (NAME_YAODIAN100_NICKNAME.equals(name)) { //
		// 60 * 24 = 1天
		// 1 天 * 365
		c.setMaxAge(60 * 24 * 365);
	}
	if (StringUtils.equals("cartid", name)) {
		c.setMaxAge(CARTID_COOKIE_TIMEOUT);
	}
	if (time_out != 0) {
		c.setMaxAge(time_out);
	}
	c.setPath(CONTEXT_PATH);
	getResponse().addCookie(c);
}
        private static HttpServletRequest getRequest() {
        	return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        }
        private static HttpServletResponse getResponse() {
//            ServletWebRequest swr=new ServletWebRequest(getRequest());
//            return swr.getResponse();
            return responseInThread.get();
        }
}
